<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QSemaphore Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">&#160;&#160;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&#160;&#183; <a href="classes.html"><font color="#004faf">All Classes</font></a>&#160;&#183; <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QSemaphore Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1><p>The QSemaphore class provides a general counting semaphore.
<a href="#details">More...</a></p>

<h3>Methods</h3><ul><li><div class="fn" /><b><a href="qsemaphore.html#QSemaphore">__init__</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;0)</li><li><div class="fn" /><b><a href="qsemaphore.html#acquire">acquire</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</li><li><div class="fn" />int <b><a href="qsemaphore.html#available">available</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qsemaphore.html#release">release</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</li><li><div class="fn" />bool <b><a href="qsemaphore.html#tryAcquire">tryAcquire</a></b> (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</li><li><div class="fn" />bool <b><a href="qsemaphore.html#tryAcquire-2">tryAcquire</a></b> (<i>self</i>, int&#160;<i>n</i>, int&#160;<i>timeout</i>)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QSemaphore class provides a general counting semaphore.</p>
<p>A semaphore is a generalization of a mutex. While a mutex can
only be locked once, it's possible to acquire a semaphore multiple
times. Semaphores are typically used to protect a certain number of
identical resources.</p>
<p>Semaphores support two fundamental operations, <a href="qsemaphore.html#acquire">acquire</a>() and <a href="qsemaphore.html#release">release</a>():</p>
<ul>
<li>acquire(<i>n</i>) tries to acquire <i>n</i> resources. If there
aren't that many resources available, the call will block until
this is the case.</li>
<li>release(<i>n</i>) releases <i>n</i> resources.</li>
</ul>
<p>There's also a <a href="qsemaphore.html#tryAcquire">tryAcquire</a>() function that returns
immediately if it cannot acquire the resources, and an <a href="qsemaphore.html#available">available</a>() function that returns
the number of available resources at any time.</p>
<p>Example:</p>
<pre class="cpp">
 <span class="type">QSemaphore</span> sem(<span class="number">5</span>);      <span class="comment">// sem.available() == 5</span>

 sem<span class="operator">.</span><a href="qsemaphore.html#acquire">acquire</a>(<span class="number">3</span>);         <span class="comment">// sem.available() == 2</span>
 sem<span class="operator">.</span><a href="qsemaphore.html#acquire">acquire</a>(<span class="number">2</span>);         <span class="comment">// sem.available() == 0</span>
 sem<span class="operator">.</span><a href="qsemaphore.html#release">release</a>(<span class="number">5</span>);         <span class="comment">// sem.available() == 5</span>
 sem<span class="operator">.</span><a href="qsemaphore.html#release">release</a>(<span class="number">5</span>);         <span class="comment">// sem.available() == 10</span>

 sem<span class="operator">.</span><a href="qsemaphore.html#tryAcquire">tryAcquire</a>(<span class="number">1</span>);      <span class="comment">// sem.available() == 9, returns true</span>
 sem<span class="operator">.</span><a href="qsemaphore.html#tryAcquire">tryAcquire</a>(<span class="number">250</span>);    <span class="comment">// sem.available() == 9, returns false</span>
</pre>
<p>A typical application of semaphores is for controlling access to
a circular buffer shared by a producer thread and a consumer
thread. The <a href="threads-semaphores.html">Semaphores</a>
example shows how to use QSemaphore to solve that problem.</p>
<p>A non-computing example of a semaphore would be dining at a
restaurant. A semaphore is initialized with the number of chairs in
the restaurant. As people arrive, they want a seat. As seats are
filled, <a href="qsemaphore.html#available">available</a>() is
decremented. As people leave, the <a href="qsemaphore.html#available">available</a>() is incremented,
allowing more people to enter. If a party of 10 people want to be
seated, but there are only 9 seats, those 10 people will wait, but
a party of 4 people would be seated (taking the available seats to
5, making the party of 10 people wait longer).</p>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QSemaphore" />QSemaphore.__init__ (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;0)</h3><p>Creates a new semaphore and initializes the number of resources
it guards to <i>n</i> (by default, 0).</p>
<p><b>See also</b> <a href="qsemaphore.html#release">release</a>()
and <a href="qsemaphore.html#available">available</a>().</p>


<h3 class="fn"><a name="acquire" />QSemaphore.acquire (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</h3><p>Tries to acquire <tt>n</tt> resources guarded by the semaphore.
If <i>n</i> &gt; <a href="qsemaphore.html#available">available</a>(), this call will block
until enough resources are available.</p>
<p><b>See also</b> <a href="qsemaphore.html#release">release</a>(),
<a href="qsemaphore.html#available">available</a>(), and <a href="qsemaphore.html#tryAcquire">tryAcquire</a>().</p>


<h3 class="fn"><a name="available" />int QSemaphore.available (<i>self</i>)</h3><p>Returns the number of resources currently available to the
semaphore. This number can never be negative.</p>
<p><b>See also</b> <a href="qsemaphore.html#acquire">acquire</a>()
and <a href="qsemaphore.html#release">release</a>().</p>


<h3 class="fn"><a name="release" />QSemaphore.release (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</h3><p>Releases <i>n</i> resources guarded by the semaphore.</p>
<p>This function can be used to "create" resources as well. For
example:</p>
<pre class="cpp">
 <span class="type"><a href="qsemaphore.html">QSemaphore</a></span> sem(<span class="number">5</span>);      <span class="comment">// a semaphore that guards 5 resources</span>
 sem<span class="operator">.</span><a href="qsemaphore.html#acquire">acquire</a>(<span class="number">5</span>);         <span class="comment">// acquire all 5 resources</span>
 sem<span class="operator">.</span>release(<span class="number">5</span>);         <span class="comment">// release the 5 resources</span>
 sem<span class="operator">.</span>release(<span class="number">10</span>);        <span class="comment">// "create" 10 new resources</span>
</pre>
<p><b>See also</b> <a href="qsemaphore.html#acquire">acquire</a>()
and <a href="qsemaphore.html#available">available</a>().</p>


<h3 class="fn"><a name="tryAcquire" />bool QSemaphore.tryAcquire (<i>self</i>, int&#160;<i>n</i>&#160;=&#160;1)</h3><p>Tries to acquire <tt>n</tt> resources guarded by the semaphore
and returns true on success. If <a href="qsemaphore.html#available">available</a>() &lt; <i>n</i>, this
call immediately returns false without acquiring any resources.</p>
<p>Example:</p>
<pre class="cpp">
 <span class="type"><a href="qsemaphore.html">QSemaphore</a></span> sem(<span class="number">5</span>);      <span class="comment">// sem.available() == 5</span>
 sem<span class="operator">.</span>tryAcquire(<span class="number">250</span>);    <span class="comment">// sem.available() == 5, returns false</span>
 sem<span class="operator">.</span>tryAcquire(<span class="number">3</span>);      <span class="comment">// sem.available() == 2, returns true</span>
</pre>
<p><b>See also</b> <a href="qsemaphore.html#acquire">acquire</a>().</p>


<h3 class="fn"><a name="tryAcquire-2" />bool QSemaphore.tryAcquire (<i>self</i>, int&#160;<i>n</i>, int&#160;<i>timeout</i>)</h3><p>Tries to acquire <tt>n</tt> resources guarded by the semaphore
and returns true on success. If <a href="qsemaphore.html#available">available</a>() &lt; <i>n</i>, this
call will wait for at most <i>timeout</i> milliseconds for
resources to become available.</p>
<p>Note: Passing a negative number as the <i>timeout</i> is
equivalent to calling <a href="qsemaphore.html#acquire">acquire</a>(), i.e. this function will
wait forever for resources to become available if <i>timeout</i> is
negative.</p>
<p>Example:</p>
<pre class="cpp">
 <span class="type"><a href="qsemaphore.html">QSemaphore</a></span> sem(<span class="number">5</span>);            <span class="comment">// sem.available() == 5</span>
 sem<span class="operator">.</span><a href="qsemaphore.html#tryAcquire">tryAcquire</a>(<span class="number">250</span><span class="operator">,</span> <span class="number">1000</span>);    <span class="comment">// sem.available() == 5, waits 1000 milliseconds and returns false</span>
 sem<span class="operator">.</span><a href="qsemaphore.html#tryAcquire">tryAcquire</a>(<span class="number">3</span><span class="operator">,</span> <span class="number">30000</span>);     <span class="comment">// sem.available() == 2, returns true without waiting</span>
</pre>
<p><b>See also</b> <a href="qsemaphore.html#acquire">acquire</a>().</p>
<address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt&#160;snapshot-4.9.5-9eb6aac99275 for X11</td><td align="center" width="50%">Copyright &#169; <a href="http://www.riverbankcomputing.com">Riverbank&#160;Computing&#160;Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2012</td><td align="right" width="25%">Qt&#160;4.8.3</td></tr></table></div></address></body></html>